//
//  ViewController.m
//  搜索商品
//
//  Created by Mac on 15/11/22.
//  Copyright © 2015年 com.moruiwei.www. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "MJRefresh.h"
#import "MJExtension.h"

#import "MOshoppingCell.h"

#import "MOShoppingModel.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *array;

@property (nonatomic,copy) NSString *shoppingName;

@property (nonatomic,assign) int page;
@property (nonatomic,assign) int pageSize;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *viewss = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 30)];
    
    
    viewss.backgroundColor = [UIColor redColor];
    
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    searchBar.frame = CGRectMake(40, 0, 210, 30);
    searchBar.backgroundColor = [UIColor whiteColor];
    [viewss addSubview:searchBar];
    
    
    self.navigationItem.titleView = viewss;
    
    
    //self.page = 1;
    self.pageSize = 13;
    self.shoppingName = @"";
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    
    //下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(setCell)];
    // 自动下拉刷新
    [self.tableView headerBeginRefreshing];
    //上拉刷新
    [self.tableView addFooterWithTarget:self action:@selector(AddCell)];
}
//点击了搜索按钮会执行这个方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.shoppingName = searchBar.text;
    
    //NSLog(@"%@",searchBar);
    
    [self setCell];
}
//下拉
- (void)setCell
{
    self.page = 1;
    
    [self setCellSource];
}
- (void)AddCell
{
    self.page++;
    
    [self setCellSource];
}
- (void)setCellSource
{
    [MBProgressHUD showMessage:@"加载中....."];
    
    //获取商品接口
    NSString *urlStr = @"http://120.25.134.185:8081/mapi/Goods/List";
    NSDictionary *dic = @{
                          @"cityCode":@"450100",
                          @"page":[NSString stringWithFormat:@"%d",self.page],
                          @"pageSize":[NSString stringWithFormat:@"%d",self.pageSize],
                          @"name":self.shoppingName
                          };
    //NSLog(@"%@",dic);
    // 创建请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr POST:urlStr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        // 获取字典数组
        NSArray *dictArr = responseObject[@"data"];
        // 把字典数组转换成模型数组
        NSArray *statuses = (NSMutableArray *)[MOShoppingModel objectArrayWithKeyValuesArray:dictArr];
        
        if (self.page == 1) {
            self.array = nil;
        }
        // 把数组中的元素添加进去
        [self.array addObjectsFromArray:statuses];
        
        [MBProgressHUD hideHUD];
        
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];

        [self.tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (self.page != 1) {
            self.page--;
        }
        
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
    }];
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1 创建可重用cell对象
    static NSString *reuseId = @"Cell";
    MOshoppingCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MOshoppingCell" owner:nil options:nil] lastObject];
    }
    
    cell.shoppingModel = self.array[indexPath.row];
    
    return cell;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
        
        _tableView.backgroundColor = [UIColor redColor];
        
    }
    return _tableView;
}
- (NSMutableArray *)array
{
    if (_array == nil) {
        _array = [NSMutableArray array];
    }
    return _array;
}

@end
